package com.learning.jquizenglish.screen.viewcontroller;

import java.util.ArrayList;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.learning.jquizenglish.JQuizAppConfig;
import com.learning.jquizenglish.R;
import com.learning.jquizenglish.model.DoQuizSession;
import com.learning.jquizenglish.model.QuestionModel;
import com.learning.jquizenglish.screen.DoQuizScreen;
import com.learning.jquizenglish.screen.QuestionFragment;

public class DoQuizView {

	private ViewPager mViewPager;
	private Button	btnPlayStop;
	private ImageButton btnFinish;
	private TextView	txtIndicator;
	private FragmentActivity mFragActivity;
	private DoQuizScreen mDoQuizScreen;
	private ArrayList<QuestionModel> mLiQuestionModels;
	
	public DoQuizView(DoQuizScreen fragScreen) {
		mDoQuizScreen = fragScreen;
	}
	
	public void init(FragmentActivity fragActivity,View contentView){
		mFragActivity = fragActivity;
		mViewPager = (ViewPager) contentView.findViewById(R.id.pager);
		btnPlayStop	= (Button) contentView.findViewById(R.id.btnPlayStop);
		btnFinish	= (ImageButton) contentView.findViewById(R.id.btn_finish);
		txtIndicator = (TextView) contentView.findViewById(R.id.tv_indicator);
		initData();
	}
	
	private void initData(){
		mLiQuestionModels = DoQuizSession.getInstance().getListQuestions();
		if(mLiQuestionModels==null ||mLiQuestionModels.size()==0)
			return;
		txtIndicator.setText(Html.fromHtml(getPageIndicator(1)));
		QuestionSlideAdapter questionSlideAdapter = new QuestionSlideAdapter(mFragActivity.getSupportFragmentManager());
		mViewPager.setAdapter(questionSlideAdapter);
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int pos) {
				txtIndicator.setText(Html.fromHtml(getPageIndicator(pos+1)));
				if(pos == (mLiQuestionModels.size()-1)){
					btnFinish.setVisibility(View.VISIBLE);
				}else{
					btnFinish.setVisibility(View.GONE);
				}
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				
			}
		});
		if(!JQuizAppConfig.IS_LISTENING_APP){
			btnPlayStop.setVisibility(View.GONE);
		}
	}
	
	public String getPageIndicator(int questionNumber){
		String ret = "";
		String temp1 = "[";
		String temp2 = "]";
		int size = mLiQuestionModels.size();
		for (int i = 1; i <= size; i++) {
			if(i==questionNumber){
				ret = ret + " <font color='#000000'>" + temp1 + i + temp2 + "</font> ";
			}else{
				ret = ret + " <font color='#000000'>" +i+ "</font> ";
			}
		}
		return ret;
	}
	
	public class QuestionSlideAdapter extends FragmentStatePagerAdapter{

		public QuestionSlideAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			return QuestionFragment.create(position);
		}

		@Override
		public int getCount() {
			return mLiQuestionModels.size();
		}
		
	}
}
